package edu.neu.chenhan.fragment.cooperation;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Toast;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import edu.neu.chenhan.R;
import edu.neu.chenhan.model.Meeting;
import java.io.Serializable;

public class MeetingDetailFragment extends Fragment {
    private static final String ARG_MEETING = "meeting";

    private Button btnSubmitForm;
    private ImageView btnCloseForm;
    private EditText etCompany, etName, etPhone, etEmail, etArrivalMethod, etArrivalTrain, etArrivalTime;
    private RadioGroup rgGender;
    private RadioButton rbMale, rbFemale;
    private View attendanceFormCard, backgroundOverlay;
    private Meeting meeting;

    public static MeetingDetailFragment newInstance(Meeting meeting) {
        MeetingDetailFragment fragment = new MeetingDetailFragment();
        Bundle args = new Bundle();
        args.putSerializable(ARG_MEETING, meeting);
        fragment.setArguments(args);
        return fragment;
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.activity_attendance_form, container, false);

        // 初始化参会回执表单视图
        backgroundOverlay = view.findViewById(R.id.background_overlay);
        btnSubmitForm = view.findViewById(R.id.btn_submit);
        etCompany = view.findViewById(R.id.et_company);
        etName = view.findViewById(R.id.et_name);
        rgGender = view.findViewById(R.id.rg_gender);
        rbMale = view.findViewById(R.id.rb_male);
        rbFemale = view.findViewById(R.id.rb_female);
        etPhone = view.findViewById(R.id.et_phone);
        etEmail = view.findViewById(R.id.et_email);
        etArrivalMethod = view.findViewById(R.id.et_arrival_method);
        etArrivalTrain = view.findViewById(R.id.et_arrival_train);
        etArrivalTime = view.findViewById(R.id.et_arrival_time);

        // 获取 Meeting 对象
        if (getArguments() != null) {
            Serializable serializable = getArguments().getSerializable(ARG_MEETING);
            if (serializable instanceof Meeting) {
                meeting = (Meeting) serializable;
            }
        }

        // 默认显示表单
        attendanceFormCard.setVisibility(View.VISIBLE);
        backgroundOverlay.setVisibility(View.VISIBLE);

        // 关闭按钮事件
        btnCloseForm.setOnClickListener(v -> {
            clearForm();
            requireActivity().getSupportFragmentManager().popBackStack();
        });

        // 提交按钮事件
        btnSubmitForm.setOnClickListener(v -> {
            if (validateForm()) {
                String gender = rbMale.isChecked() ? "男" : rbFemale.isChecked() ? "女" : "";
                String attendanceDetails = "会议: " + (meeting != null && meeting.getName() != null ? meeting.getName() : "未知") +
                        "\n单位: " + etCompany.getText().toString() +
                        "\n姓名: " + etName.getText().toString() +
                        "\n性别: " + gender +
                        "\n手机号码: " + etPhone.getText().toString() +
                        "\n电子邮箱: " + etEmail.getText().toString() +
                        "\n到达方式: " + etArrivalMethod.getText().toString() +
                        "\n到达车次: " + etArrivalTrain.getText().toString() +
                        "\n到达时间: " + etArrivalTime.getText().toString();
                Toast.makeText(requireContext(), "参会信息已提交:\n" + attendanceDetails, Toast.LENGTH_LONG).show();
                clearForm();
                requireActivity().getSupportFragmentManager().popBackStack();
            }
        });

        return view;
    }

    // 验证表单输入
    private boolean validateForm() {
        if (etCompany.getText().toString().trim().isEmpty()) {
            etCompany.setError("请输入单位名称");
            return false;
        }
        if (etName.getText().toString().trim().isEmpty()) {
            etName.setError("请输入姓名");
            return false;
        }
        if (!rbMale.isChecked() && !rbFemale.isChecked()) {
            Toast.makeText(requireContext(), "请选择性别", Toast.LENGTH_SHORT).show();
            return false;
        }
        if (etPhone.getText().toString().trim().isEmpty()) {
            etPhone.setError("请输入手机号码");
            return false;
        }
        if (etEmail.getText().toString().trim().isEmpty()) {
            etEmail.setError("请输入电子邮箱");
            return false;
        }
        return true;
    }

    // 清空表单
    private void clearForm() {
        etCompany.setText("");
        etName.setText("");
        rgGender.clearCheck();
        etPhone.setText("");
        etEmail.setText("");
        etArrivalMethod.setText("");
        etArrivalTrain.setText("");
        etArrivalTime.setText("");
    }
}